<?php
namespace Home\Controller;
use Think\Controller;
class VisitorsController extends CommonController {

    //访问他人空间时  显示的被访问者的主要信息  需要传入被访问者的唯一标识
    public function index(){
        //当是自己使用访问者访问自己时
        // if ($_GET['id'] = $_SESSION['home']['uid']) {
        //     $this -> success('正在跳转到您的主页~',U('News/index'));die;
        // }
        //得到被访问者的ID
        $id = I('get.id',0,int);
        if($id){
            //实例化user_info
            $user_info = M('user_info');
            //得到被访问者的信息
            $res = $user_info -> where('user_id ='.$id) -> select();
            //计算得到用户等级
            $grade = floor( sqrt( $res[0]['exp'] ) );

            if($id!=$_SESSION['home']['uid']){
                $pug=M('Pug');
                $count=$pug->where('user_id_a='.$_SESSION['home']['uid'].' and user_id_b='.$id)->count();
                if($count){
                    $data=array(
                        'time'=>time() //访问时间
                    );
                    $pug->where('user_id_a='.$_SESSION['home']['uid'].' and user_id_b='.$id)->save($data);
                }else{
                    $data=array(
                        'user_id_a'=>$_SESSION['home']['uid'], //访问者id
                        'user_id_b'=>$id, //被访问者id
                        'time'=>time() //访问时间
                    );
                    $pug->add($data);
                }
            }

            $this -> assign('info',$res[0]);
            $this -> assign('grade',$grade);
            $this -> assign('exp',$res[0]['exp']);
            $this -> assign('test',$res[0]['nickname'].'的个人主页');
            $this->display();
        }
    }

    //访问他人空间时 显示被访问者的动态 需要传入被访问者的唯一标识
    public function news(){
        //得到被访问者的ID
        $id = $_GET['id'];
        //实例化user_info
        $user_info = M('user_info');
        //得到被访问者的信息
        $res = $user_info -> where('user_id ='.$id) -> select();
        $this -> assign('info',$res[0]);
        $this -> assign('uid',$_SESSION['home']['uid']);
        $this -> assign('nickname',$_SESSION['home']['nickname']);
        $this -> assign('test',$res[0]['nickname'].'的动态中心');
        $this->display();
    }

    //访问他人空间时 显示被访问者的相册 需要传入被访问者的唯一标识
    public function albums(){
        $this -> assign('test','访问他人空间时 显示被访问者的相册 需要传入被访问者的唯一标识');
        $this->display();
    }

    //访问他们空间时  显示被访问者的相片 需要传入被访问者的唯一标识
    public function image(){
        $this -> assign('test','访问他们空间时  显示被访问者的相片 需要传入被访问者的唯一标识');
        $this->display();
    }

    //实现动态瀑布流需要使用方法
    public function ajaxpull() {
        //只有使用ajax访问的时候 才是被允许的
        if (IS_AJAX) {
            //得到需要传输的数据的数量
            $num = $_POST['page'];
            //得到被访问者的ID
            $id = $_POST['vid'];
            // var_dump($_POST);die;
            //得到好友和自己的信息
            $news = M('news');
            $info = $news
                ->field('zn.news_id,zn.turn_content,zn.user_id,zn.content,zn.time,zn.isturn,zn.news_zan,zu.nickname as zunickname,yu.nickname as yunickname,zu.photo,zu.user_id as zui,yu.user_id as yui')
                ->where('zu.user_id ='.$id)
                ->table('( SELECT zn1.user_id AS zn1id,zn2.user_id AS zn2id FROM zz_news AS zn1,zz_news AS zn2 WHERE ( zn1.isturn = zn2.news_id ) GROUP BY zn1id ) AS t')
                ->join('zz_user_info AS zu ON zu.user_id = t.zn1id')
                ->join('zz_user_info AS yu ON yu.user_id = t.zn2id')
                ->join('zz_news AS zn ON zn.user_id = t.zn1id')
                ->page($num, 5)
                ->order('zn.time DESC')
                ->select();
            // var_dump($info);
            // echo $news->_sql();die;
            //纠正转发BUG   现修正 :yunickname yui
            foreach ($info as $key => $val) {
                //得到原动态的ID
                $datanick['news_id'] = $info[$key]['isturn'];
                $newsfornick         = M('news');
                $yunickname          = $newsfornick
                    ->field('zz_user_info.nickname,zz_user_info.user_id')
                    ->where($datanick)
                    ->join('zz_user_info ON zz_user_info.user_id = zz_news.user_id')
                    ->select();
                // echo $newsfornick -> _sql();
                // var_dump($yunickname);die();
                $info[$key]['yunickname'] = $yunickname[0]['nickname'];
                $info[$key]['yui'] = $yunickname[0]['user_id'];
            }
            //将查询结果转换成json格式
            $this->ajaxReturn($info);
        } else {
            $this->error('非法操作');
        }
    }

}
